package com.jiawa.train.business.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.TrainStation;
import com.jiawa.train.business.domain.TrainStationExample;
import com.jiawa.train.business.mapper.TrainStationMapper;
import com.jiawa.train.business.req.TrainSaveReq;
import com.jiawa.train.business.req.TrainStationQueryReq;
import com.jiawa.train.business.resp.TrainStationQueryResp;
import com.jiawa.train.business.service.TrainStationService;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zzl
 * @version 1.0
 */
@Service
public class TrainStationServiceImpl implements TrainStationService {
    private static final Logger LOG = LoggerFactory.getLogger(TrainStationServiceImpl.class);

    @Resource
    private TrainStationMapper trainStationMapper;

    /**
     * 新增一条高铁经停车站信息
     * @param req
     */
    @Override
    public void save(TrainSaveReq req) {
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);
        if(ObjectUtil.isNull(trainStation.getId())) {
            if(isExist(trainStation)) {
                throw new BusinessException(BusinessExceptionEnum.TRAIN_STATION_IS_EXIST);
            } else {
                trainStation.setId(SnowUtil.getSnowflakeNextId());
                trainStation.setCreateTime(now);
                trainStation.setUpdateTime(now);
                trainStationMapper.insert(trainStation);
            }
        } else {
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKeySelective(trainStation);
        }
    }

    /**
     * 分页查询经停车站信息，排序规则根据高铁车次和经停车站index升序
     * @param req
     * @return
     */
    @Override
    public PageResp<TrainStationQueryResp> queryList(TrainStationQueryReq req) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("train_code asc, `index` asc");
        TrainStationExample.Criteria criteria = trainStationExample.createCriteria();
        if(ObjectUtil.isNotNull(req.getTrainCode())) {
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<TrainStation> trainStationList = trainStationMapper.selectByExample(trainStationExample);

        PageInfo<TrainStation> pageInfo = new PageInfo<>(trainStationList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<TrainStationQueryResp> trainStationQueryRespList = BeanUtil.copyToList(trainStationList, TrainStationQueryResp.class);
        PageResp<TrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());;
        pageResp.setList(trainStationQueryRespList);
        return pageResp;
    }

    /**
     * 根据id删除经停车站信息
     * @param id
     */
    @Override
    public void delete(Long id) {
        trainStationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据高铁车次查询经停车站信息
     * @param code
     * @return
     */
    @Override
    public List<TrainStation> selectByTrainCode(String code) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria()
                .andTrainCodeEqualTo(code);
        return trainStationMapper.selectByExample(trainStationExample);
    }

    /**
     * 判断经停车站是否存在
     * @param trainStation
     * @return
     */
    private boolean isExist(TrainStation trainStation) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria()
                .andTrainCodeEqualTo(trainStation.getTrainCode())
                .andIndexEqualTo(trainStation.getIndex());
        trainStationExample.or(trainStationExample.createCriteria()
                .andTrainCodeEqualTo(trainStation.getTrainCode())
                .andNameEqualTo(trainStation.getName()));
        List<TrainStation> list = trainStationMapper.selectByExample(trainStationExample);
        if(CollUtil.isNotEmpty(list)) return true;
        else return false;
    }
}
